﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Yeap.Kernel.Utils;
using Yeap.Kernel.Utils.Enums;

namespace Yeap.Kernel.Storage
{
    public class Student
    {
        #region [Public Properties]

        public Int32 Id { get; set; }
        public String Login { get; set; }
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public Int32 Course { get; set; }
        public Dictionary<String, String> Properties { get; set; }

        #endregion

        #region [Private Properties]

        IWorkRepository workRepository = new WorkRepository();
        IUserRepository userRepository = new UserRepository();
        IUserWorkRepository userWorkRepository = new UserWorkRepository();

        public User CurrentUser
        {
            get { return userRepository.GetById(Id); }
        }

        #endregion

        #region [Bool possibilities]

        public String FullName
        {
            get { return LastName + " " + FirstName; }
        }

        public bool IsPossibleToApply(int workId)
        {
            var currentWork = workRepository.GetById(workId);

            if (currentWork.Year != SiteSettingPool.CurrentYear)
            {
                return false;
            }

            if (currentWork.Course != this.Course)
            {
                return false;
            }

            if (currentWork.GetWorkType() != WorkTypes.Available)
            {
                return false;
            }

            if (CurrentUser.UserWorks.Any(uw => uw.Type ==AssignmentTypes.Agreement || uw.Type == AssignmentTypes.Finished))
            {
                return false;
            }

            if (CurrentUser.UserWorks.Any(uw => uw.Type==AssignmentTypes.StudentProposal && uw.UserId == CurrentUser.Id))
            {
                return false;
            }

            return true;
        }

        public bool IsAssignedToAnyWork()
        {
            return
                userWorkRepository.Query(uw =>
                    uw.UserId.Equals(CurrentUser.Id) &&
                    (uw.Type == AssignmentTypes.Agreement || uw.Type == AssignmentTypes.Finished)
                    ).Any();
        }

        public bool IsProposedToAnyWork()
        {
            return userWorkRepository.Query(uw => uw.UserId.Equals(CurrentUser.Id)).Any();
        }

        public bool CanUploadFilesToWork(int workId)
        {
            var currentWork = workRepository.GetById(workId);

            if (CurrentUser.UserWorks.Where(uw => uw.Type == AssignmentTypes.Agreement || uw.Type == AssignmentTypes.Finished).Where(uw => uw.WorkId == workId).Any())
            {
                return true;
            }

            return false;
        }

        

        #endregion
    }
}